//
//  SSMyCouponController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/3/3.
//

import UIKit
import SwiftyJSON


class SSMyCouponController: UIViewController {
    
    @IBOutlet weak var pagetitleView: PageTitleView!
    @IBOutlet weak var pagecontentView: PageContentView!

    var vcs = [SSMyCouponSubController]()
    var myCouponClosure: MyCouponClosure?     // 闭包
    var isUserOrder:Bool = false
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
    setupUI()
     network()
    }

    
    func setupUI() {
        
        title = "我的优惠券"
        
        initChileViewControllers()
        
//        pagetitleView.backgroundColor = UIColor.blue
        let styleTitle = PageStyle()
        styleTitle.titleViewBackgroundColor = UIColor.clear
        styleTitle.titleFont = UIFont.systemFont(ofSize: 14, weight: .medium)
        styleTitle.titleColor = UIColor.black
        styleTitle.titleSelectedColor = UIColor.themeColor
        styleTitle.coverViewRadius = 0
        styleTitle.coverMargin = 5
        styleTitle.isShowCoverView = false
        styleTitle.isShowBottomLine = true
        styleTitle.bottomLineColor = UIColor.themeColor
        styleTitle.bottomLineHeight = 2
        styleTitle.bottomLineWidth = 0
        let titles = ["未使用", "已使用","已过期"]//, "咨询"
        pagetitleView.initData(style: styleTitle, titles: titles, currentIndex: 0)
        pagecontentView.initData(style: styleTitle, childViewControllers: vcs, currentIndex: 0)
        pagetitleView.delegate = pagecontentView
        pagecontentView.delegate = pagetitleView
    }
    
    
    func initChileViewControllers(){
        let vc1 = SSMyCouponSubController()
        vcs.append(vc1)
        vc1.title = "未使用"
        if self.isUserOrder {
            vc1.closure { number in
                if self.myCouponClosure != nil {
                    self.myCouponClosure!(number)
                }
            }
        }
        vc1.view.backgroundColor = UIColor.white
        self.addChild(vc1)
        
        let vc2 = SSMyCouponSubController()
//        vc1.title = "已使用"
        vc2.view.backgroundColor = UIColor.white
        vcs.append(vc2)
        self.addChild(vc2)
        
        let vc3 = SSMyCouponSubController()
//        vc1.title = "已过期"
        vc3.view.backgroundColor = UIColor.white
        vcs.append(vc3)
        self.addChild(vc3)
    }
    
    func closure(_ finished: @escaping MyCouponClosure) {
        myCouponClosure = finished
    }
    
    func network() {
        let param = [
                    "phoneNo": kUserPhone,
                ] as [String:AnyObject]
 
        SSProgressHUD.share.show()
        HttpConnectTools.share.request(urlString: APIUrl.share.getUserCoupon_URL, parameters: param) { (result, error) in
            SSProgressHUD.share.dismiss()
            if let _ = result{
                let json = JSON(parseJSON: result!)
                let listNotUsed = json["data"]["listNotUsed"].arrayValue
                let listUsed = json["data"]["listUsed"].arrayValue
                let listExpired = json["data"]["listExpired"].arrayValue
                var notUseditems: Array<SSCoupon> = []
                var usedUseditems: Array<SSCoupon> = []
                var expireditems: Array<SSCoupon> = []
                listNotUsed.forEach{
                    (num:JSON) in
                    let obj = SSCoupon()
                    obj.imgUrl = num["imgUrl"].stringValue
                    obj.couponId = num["couponId"].stringValue
                    obj.userCouponId = num["userCouponId"].stringValue
                    obj.money = num["money"].stringValue
                    obj.fullMoney = num["fullMoney"].stringValue
                    notUseditems.append(obj)
                }
                listUsed.forEach{
                    (num:JSON) in
                    let obj = SSCoupon()
                    obj.imgUrl = num["imgUrl"].stringValue
                    obj.couponId = num["couponId"].stringValue
                    usedUseditems.append(obj)
                }
                listExpired.forEach{
                    (num:JSON) in
                    let obj = SSCoupon()
                    obj.imgUrl = num["imgUrl"].stringValue
                    obj.couponId = num["couponId"].stringValue
                    expireditems.append(obj)
                }
                print("")
                self.vcs[0].setData(data: notUseditems)
                self.vcs[1].setData(data: usedUseditems)
                self.vcs[2].setData(data: expireditems)
                
            }
    }
  
    }

}


